﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using NLog;

namespace AllInOnePluginBL.SiteScrappers
{
    /// <summary>
    /// Summary description for ReshetParserJob
    /// </summary>
    public class ReshetParserJob : ISiteScapper
    {
        #region Private Members
        private const string CATEGORIES_URL = "http://reshet.ynet.co.il/Video/.aspx?fullChapterMore=1";
        private const string NAME = "Reshet";

        private readonly Logger _log = LogManager.GetLogger("ReshetParserJob");
        private readonly PluginDBHandler _pluginDBHandler = PluginDBHandler.Instance;
        #endregion

        public string Name { get; private set; }

        public JobType JobType { get; set; }

        public long JobID { get; set; }

        public ReshetParserJob()
        {
            this.Name = NAME;
        }

        #region Implementation of IJob
        public void Execute(int pagesToRun)
        {
            _log.Info(string.Format("Start Executing ReshetParserJob at {0}", DateTime.Now.ToString("r")));

            // Grab passed parameters
            //var data = context.JobDetail.JobDataMap;
            //var pagesToScan = 3;// (int)data.Get(Helpers.Common.PAGES_TO_SCAN);

            //var jobQueue = data.Get("JobQueue") as JobQueue;
            //if (jobQueue != null)
            //    jobQueue.SetJobRun(typeof(MyVodTVParserJob));

            //var lastPageNumber = 30;// GetLastPageLink(DEFAULT_URL);
            //pagesToScan = (pagesToScan == 0 || pagesToScan > lastPageNumber) ? lastPageNumber : pagesToScan;

            //for (var i = pagesToScan; i > 0; i--)
            //{
            //    var url = string.Format("{0}page/{1}", DEFAULT_URL, i);
            //    this.GetMovies(url);
            //}

            GetCategories();
            _log.Info(string.Format("End Executing ReshetParserJob at {0}", DateTime.Now.ToString("r")));
        }
        #endregion

        private void GetCategories()
        {
            try
            {
                var pageContent = Utils.GetPage(CATEGORIES_URL, Encoding.UTF8);

                const string categoriesRegEx = "\\['sId','(\\d+)'\\],\\['Text','.*?'\\],\\['BoneName','(.*?)'\\]";
                var matches = Regex.Matches(pageContent, categoriesRegEx, RegexOptions.Singleline);
                foreach (Match match in matches)
                {
                    var moviePageLink = match.Groups[1].Value;
                    var movieTitle = match.Groups[2].Value;

                    if (_pluginDBHandler.IsVideoExists(moviePageLink))
                        continue;

                    //var video = FillMovieInfo(movieTitle, moviePageLink);

                    //if (video.Links.Count == 0)
                    //{
                    //    _log.WarnFormat("{0} Has no links. not saving it", movieTitle);
                    //    return;
                    //}

                    //_pluginDBHandler.AddNewVideo(video);
                }
            }
            catch (Exception ex)
            {
                _log.ErrorException(string.Format("Error in MyVodParser::GetMovies. url:{0}", CATEGORIES_URL), ex);
            }
        }
    }
}